﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace Utility.Web
{
    public class WebMail
    {
        public WebMail() { }
        private List<string> _listStringEmail;
        private string _subjec;
        private string _content;
        private bool _isHtml;
        private string _userName;
        private string _mailPass;
        private string _smtpHost;
        private int _mailPort;
        private string _mailFrom;

        public List<string> ListMailSend
        {
            get { return _listStringEmail; }
            set { _listStringEmail = value; }
        }
        public string Subject
        {
            get { return _subjec; }
            set { _subjec = value; }
        }
        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }
        public bool IsHtml
        {
            get { return _isHtml; }
            set { _isHtml = value; }
        }
        public string userName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        public string mailPass
        {
            get { return _mailPass; }
            set { _mailPass = value; }
        }
        public string smtpHost
        {
            get { return _smtpHost; }
            set { _smtpHost = value; }
        }
        public int mailPort
        {
            get { return _mailPort; }
            set { _mailPort = value; }
        }
        public string mailFrom
        {
            get { return _mailFrom; }
            set { _mailFrom = value; }
        }

        //Send

        public static bool Send(WebMail mail, bool enableSSL)
        {
            SmtpClient smtp = new SmtpClient();
            if (!string.IsNullOrEmpty(mail.userName) && !string.IsNullOrEmpty(mail.mailPass))
                smtp.Credentials = new NetworkCredential(mail.userName, mail.mailPass);
            smtp.Host = mail.smtpHost;
            smtp.Port = mail.mailPort;
            smtp.EnableSsl = enableSSL;
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress(mail.mailFrom);
                for (int i = 0; i < mail.ListMailSend.Count; i++)
                {
                    message.To.Add(mail.ListMailSend[i]);
                }
                message.Subject = mail.Subject;
                message.Body = mail.Content;
                message.IsBodyHtml = mail.IsHtml;
                message.Priority = MailPriority.High;
                try
                {
                    smtp.Send(message);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
